package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import com.cs157a.controller.AposApplicationController;
import com.cs157a.dao.PartType;
import com.cs157a.dao.Radcrx;

public class PartNumerSelectionPanel extends JPanel {

	private static final long serialVersionUID = 20012100301L;
	private JLabel stdLabel;
	private JLabel hdLabel;
	private JLabel acLabel;
	private JLabel maxLabel;

	private JComboBox stComboBox;
	private JComboBox hdComboBox;
	private JComboBox acComboBox;
	private JComboBox maxComboBox;

	private VendorType vendorType;

	public PartNumerSelectionPanel(AposApplicationController controller,
			VendorType vendorType) {
		super();
		this.vendorType = vendorType;
		init(controller);
	}

	private void init(AposApplicationController controller) {
		stdLabel = new JLabel(PartType.STD.toString());
		hdLabel = new JLabel(PartType.HD.toString());
		acLabel = new JLabel(PartType.AC.toString());
		maxLabel = new JLabel(PartType.MAX.toString());

		List<String> partNumbers = controller.getAllPartNumbersFor(vendorType
				.toString());

		SortedComboBoxModel stdModel = new SortedComboBoxModel(
				partNumbers.toArray());
		stdModel.addElement(CommonString.BLANK_SPACE);
		stComboBox = new JComboBox(stdModel);
		stComboBox.setSelectedIndex(0);

		SortedComboBoxModel hdModel = new SortedComboBoxModel(
				partNumbers.toArray());
		hdModel.addElement(CommonString.BLANK_SPACE);
		hdComboBox = new JComboBox(hdModel);
		hdComboBox.setSelectedIndex(0);

		SortedComboBoxModel acModel = new SortedComboBoxModel(
				partNumbers.toArray());
		acModel.addElement(CommonString.BLANK_SPACE);
		acComboBox = new JComboBox(acModel);
		acComboBox.setSelectedIndex(0);

		SortedComboBoxModel maxModel = new SortedComboBoxModel(
				partNumbers.toArray());
		maxModel.addElement(CommonString.BLANK_SPACE);
		maxComboBox = new JComboBox(maxModel);
		maxComboBox.setSelectedIndex(0);

		JPanel innerPanel = new JPanel();
		innerPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.HORIZONTAL;
		innerPanel.add(stdLabel, c);

		c.gridx = 0;
		c.gridy = 1;
		innerPanel.add(hdLabel, c);

		c.gridx = 0;
		c.gridy = 2;
		innerPanel.add(acLabel, c);

		c.gridx = 0;
		c.gridy = 3;
		innerPanel.add(maxLabel, c);

		c.gridx = 1;
		c.gridy = 0;
		innerPanel.add(stComboBox, c);

		c.gridx = 1;
		c.gridy = 1;
		innerPanel.add(hdComboBox, c);

		c.gridx = 1;
		c.gridy = 2;
		innerPanel.add(acComboBox, c);

		c.gridx = 1;
		c.gridy = 3;
		innerPanel.add(maxComboBox, c);

		setLayout(new BorderLayout());
		setBorder(new TitledBorder(vendorType.toString()));
		add(innerPanel, BorderLayout.CENTER);
	}

	public void updateRadcrx(Radcrx radcrx) {
		switch (vendorType) {
		case RDIM_ARS:
			radcrx.setArs1(((String) stComboBox.getSelectedItem()).trim());
			radcrx.setArs2(((String) hdComboBox.getSelectedItem()).trim());
			radcrx.setArs3(((String) acComboBox.getSelectedItem()).trim());
			radcrx.setArs4(((String) maxComboBox.getSelectedItem()).trim());
			break;

		case RDIM_BEH:
			radcrx.setBeh1(((String) stComboBox.getSelectedItem()).trim());
			radcrx.setBeh2(((String) hdComboBox.getSelectedItem()).trim());
			radcrx.setBeh3(((String) acComboBox.getSelectedItem()).trim());
			radcrx.setBeh4(((String) maxComboBox.getSelectedItem()).trim());
			break;

		case RDIM_DAN:
			radcrx.setDan1(((String) stComboBox.getSelectedItem()).trim());
			radcrx.setDan2(((String) hdComboBox.getSelectedItem()).trim());
			radcrx.setDan3(((String) acComboBox.getSelectedItem()).trim());
			radcrx.setDan4(((String) maxComboBox.getSelectedItem()).trim());
			break;

		case RDIM_MOD:
			radcrx.setMod1(((String) stComboBox.getSelectedItem()).trim());
			radcrx.setMod2(((String) hdComboBox.getSelectedItem()).trim());
			radcrx.setMod3(((String) acComboBox.getSelectedItem()).trim());
			radcrx.setMod4(((String) maxComboBox.getSelectedItem()).trim());
			break;
		}

	}
}
